package cn.icybear.orzjisp;

import cn.icybear.orzjisp.parser.ParseException;

public class StringUtils {
	public static char parseChar(String s) throws ParseException {
		char c = s.charAt(1);
		if (c == '\\') {
			c = s.charAt(2);
			switch (c) {
			case '\\':
				c = '\\';
				break;
			case 't':
				c = '\t';
				break;
			case 'n':
				c = '\n';
				break;
			case 'r':
				c = '\r';
				break;
			case 'b':
				c = '\b';
				break;
			case 'f':
				c = 'f';
				break;
			case '\'':
				c = '\'';
				break;
			case '\"':
				c = '\"';
				break;
			default:
				throw new ParseException();
			}
		}
		return c;
	}

	public static String parseString(String s) throws ParseException {
		StringBuilder buf = new StringBuilder();
		char[] a = s.toCharArray();
		for (int i = 1; i < a.length - 1; i++) {
			char c = a[i];
			if (c == '\\') {
				c = a[++i];
				switch (c) {
				case '\\':
					c = '\\';
					break;
				case 't':
					c = '\t';
					break;
				case 'n':
					c = '\n';
					break;
				case 'r':
					c = '\r';
					break;
				case 'b':
					c = '\b';
					break;
				case 'f':
					c = 'f';
					break;
				case '\'':
					c = '\'';
					break;
				case '\"':
					c = '\"';
					break;
				default:
					throw new ParseException();
				}
			}
			buf.append(c);
		}
		return buf.toString();
	}

	public static int parseInteger(String s) {
		s = s.toLowerCase();
		if (s.startsWith("0x"))
			return Integer.parseInt(s.substring(2), 16);
		else if (s.startsWith("0")&&s.length()>1)
			return Integer.parseInt(s.substring(1), 8);
		else
			return Integer.parseInt(s);
	}

	public static double parseDouble(String s) {
		return Double.parseDouble(s);
	}

	public static boolean parseBoolean(String s) {
		return Boolean.parseBoolean(s);
	}

	public static String escapesString(String str) {
		StringBuffer retval = new StringBuffer();
		for (int i = 0; i < str.length(); i++) {
			char ch = str.charAt(i);
			switch (ch) {
			case 0:
				continue;
			case '\b':
				retval.append("\\b");
				continue;
			case '\t':
				retval.append("\\t");
				continue;
			case '\n':
				retval.append("\\n");
				continue;
			case '\f':
				retval.append("\\f");
				continue;
			case '\r':
				retval.append("\\r");
				continue;
			case '\"':
				retval.append("\\\"");
				continue;
			case '\'':
				retval.append("\\\'");
				continue;
			case '\\':
				retval.append("\\\\");
				continue;
			default:
				// if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
				// String s = "0000" + Integer.toString(ch, 16);
				// retval.append("\\u"
				// + s.substring(s.length() - 4, s.length()));
				// } else {
				 retval.append(ch);
				// }
				continue;
			}
		}
		return retval.toString();
	}

	public static String escapesChar(char c) {
		switch (c) {
		case '\0':
			return("\\0");
		case '\b':
			return("\\b");
		case '\t':
			return("\\t");
		case '\n':
			return("\\n");
		case '\f':
			return("\\f");
		case '\r':
			return("\\r");
		case '\"':
			return("\\\"");
		case '\'':
			return("\\\'");
		case '\\':
			return("\\\\");
		default:
			return String.valueOf(c);
		}
	}
}
